package com.etime.shuhuiyuan.services.course.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shuhuiyuan.dao.CourseDao;
import com.etime.shuhuiyuan.dto.CourseDto;
import com.etime.shuhuiyuan.pojo.Course;
import com.etime.shuhuiyuan.services.course.interfaces.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Locale;

@Service
public class CourseServiceImpl implements CourseService {
    //注入对象
    @Autowired
    private CourseDao courseDao;

    @Override
    public Page<CourseDto> selectCourseDtoByPage(CourseDto courseDto, long pagenum, long pagesize) {
        //分页参数
        Page<CourseDto> page = new Page<>(pagenum,pagesize);
        String coursetitle = "";
        Integer coursestate = null;
        if (courseDto!=null){
            if (courseDto.getCoursetitle()!=null && !courseDto.getCoursetitle().equalsIgnoreCase("")){
                coursetitle = courseDto.getCoursetitle();
                coursetitle = "%"+coursetitle+"%";
            }if (courseDto.getCoursestate()!=null && courseDto.getCoursestate()!=-1){
                coursestate = courseDto.getCoursestate();
            }
        }
        return courseDao.selectCourseDtoByCon(courseDto,page,coursetitle,coursestate);
    }

    @Override
    public CourseDto selectCourseDtoById(Integer courseid) {
        return courseDao.selectCourseDtoById(courseid);
    }

    @Override
    public int updateCourseDto(CourseDto courseDto) {
        //记录修改时间
        long timeSteamp = System.currentTimeMillis();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        String time = simpleDateFormat.format(timeSteamp);
        courseDto.setCourseupdatetime(time);
        //判断课程类型非空
        if (courseDto.getCoursetypeid() == null){
            courseDto.setCoursetypeid(0);
        }
        return courseDao.updateById(courseDto);
    }

    @Override
    public int insertCourseDto(CourseDto courseDto) {
        //获取当前时间为创建时间插入数据库
        long timeSteamp = System.currentTimeMillis();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        String newTime = simpleDateFormat.format(timeSteamp);
        courseDto.setCoursecreatetime(newTime);
        return courseDao.insert(courseDto);
    }
}
